package com.java2007.hotel.service.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.FoodDao;
import com.java2007.hotel.pojo.DetailOrder;
import com.java2007.hotel.dao.DetailOrderDao;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Food;
import com.java2007.hotel.service.DetailOrderService;
import org.junit.Test;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/**
 * @author csl
 * @version V1.0
 * @date 2021/01/23
 **/
public class DetailOrderServiceImpl implements DetailOrderService {
    private DetailOrderDao detailOrderDao = (DetailOrderDao) BeanFactory.getBean(Constant.DETAIL_ORDER_DAO_IMPL);
    private FoodDao foodDao = (FoodDao) BeanFactory.getBean(Constant.FOOD_DAO_IMPL);
    /**
     * 根据订单编号查询
     * @param orderNumber
     * @return
     */
    @Override
    public List<DetailOrder> selectDetailOrderByOrder(String orderNumber) throws SQLException {
        List<DetailOrder> detailOrders = detailOrderDao.selectDetailOrderByOrder(orderNumber);
        for (int i = 0; i < detailOrders.size(); i++) {
            Food food = foodDao.selectFoodById(detailOrders.get(i).getFoodId());
            if (food != null){
                detailOrders.get(i).setFoodName(food.getName());
            }else {
                detailOrders.get(i).setFoodName("菜品已删除");
            }
        }
        return detailOrders;
    }

    /**
     * 菜单详情中增加一个信息
     * @param detailOrder
     * @return
     */
    @Override
    public int addDetailOrder(DetailOrder detailOrder, int createrId) throws SQLException {
        return detailOrderDao.addDetailOrder(detailOrder,createrId);
    }
    /**
     * 修改菜单详情中数量
     * @param foodCount
     * @return
     */
    @Override
    public int updateFoodCount(Date update, int foodCount, Long foodTotalPrice, int id) throws SQLException {
        return detailOrderDao.updateFoodCount(update,foodCount,foodTotalPrice,id);
    }

    /**
     * 查询某用户为下单的菜单详情
     * @param id
     * @return
     */
    @Override
    public List<DetailOrder> selectDetailOrderByUserIdAndUnOrder(int id) throws SQLException {
        List<DetailOrder> detailOrders = detailOrderDao.selectDetailOrderByUserIdAndUnOrder(id);
        for (int i = 0; i < detailOrders.size(); i++) {
            detailOrders.get(i).setFoodName(foodDao.selectFoodById(detailOrders.get(i).getFoodId()).getName());
        }
   return detailOrders;
    }

    /**
     * 根据id查询菜单详情
     * @param id
     * @return
     */
    @Override
    public DetailOrder selectById(int id) throws SQLException {
        return detailOrderDao.selectById(id);
    }
    /**
     * 根据id删除订单详情
     * @param id
     * @return
     */
    @Override
    public int deleteDetailOrderById(int id) throws SQLException {
        return detailOrderDao.deleteDetailOrderById(id);
    }
    /**
     * 根据创建人添加菜单详情的订单编号
     * @param createrId
     * @return
     */
    @Override
    public int updateOrderNumber(int createrId, String numer) throws SQLException {
        return detailOrderDao.updateOrderNumber(createrId,numer);
    }
    /**
     * 根据订单编号查询菜单详情
     * @param foodNumber
     * @return
     */
    @Override
    public List<DetailOrder> selectByFoodNumber(String foodNumber) throws SQLException {
        List<DetailOrder> detailOrders = detailOrderDao.selectDetailOrderByOrder(foodNumber);
        for (int i = 0; i < detailOrders.size(); i++) {
            Food food = foodDao.selectFoodById(detailOrders.get(i).getFoodId());
            if (food != null){
                detailOrders.get(i).setFoodName(food.getName());
            }else {
                detailOrders.get(i).setFoodName("已删除");
            }

        }
        return detailOrders;
    }
    /**
     * 根据编号删除订单详情
     * @param number
     * @return
     */
    @Override
    public int deleteDetailOrderByNumber(String number) throws SQLException {
        return detailOrderDao.deleteDetailOrderByNumber(number);
    }

}
